package top.kaoshanji.p1ch05b;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import top.kaoshanji.p1ch04a.Dish;

import java.util.OptionalInt;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * author kaoshanji
 * ^_^
 * create time 2025/2/11 16:57
 */
public class NumericStreams {

    private static final Logger logger = LogManager.getLogger(NumericStreams.class);

    public static void main(String[] args) {

        // 代码清单-p1ch05b-107页a：映射到数值流
        logger.info("...映射到数值流...");
        int calories = Dish.menu.stream().mapToInt(Dish::getCalories).sum();
        System.out.println("Number of calories:" + calories);


        // 代码清单-p1ch05b-107页b：转换回对象流
        logger.info("...转换回对象流...");
        IntStream intStream = Dish.menu.stream().mapToInt(Dish::getCalories);
        Stream<Integer> stream = intStream.boxed();
        stream.forEach(System.out::println);


        // 代码清单-p1ch05b-108页a：默认值OptionalInt
        logger.info("...默认值OptionalInt...");
        OptionalInt maxCalories = Dish.menu.stream().mapToInt(Dish::getCalories).max();
        int max = maxCalories.orElse(1);
        System.out.println(max);


        // 代码清单-p1ch05b-108页b：取值范围
        logger.info("...取值范围...");
        IntStream evenNumbers = IntStream.rangeClosed(1, 100).filter(n -> n % 2 == 0);
        System.out.println(evenNumbers.count());



    }



}
